from bs4 import BeautifulSoup
import chardet
import ssl #用于解决https请求协议
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class GetinfoController(object):
    def __init__(self):
        # 启动模拟浏览器---------------------win下没有问题，在centos上搭建环境有问题
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--load-images=no')#关闭图片加载
        chrome_options.add_argument('--disk-cache=yes')#开启缓存
        chrome_options.add_argument('--ignore-ssl-errors=true')  ##忽略https错误
        self.browser= webdriver.Chrome(chrome_options=chrome_options)

    def index(self):
        url="https://www.ele.me/shop/160998694/rate"#抓取页面的url地址
        #self.browser.set_window_size(1366, 2000)
        self.browser.implicitly_wait(10)#显示等待外部js加载完成
        self.browser.get(url)
        time.sleep(1)#等待模拟用户行为
        self.browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
        time.sleep(2)#等待模拟用户行为
        html_data = self.browser.page_source
        self.browser.quit()#关闭浏览器
        html_obj = BeautifulSoup(html_data, 'html.parser')
        html_list = html_obj.find_all('span', attrs={"class": "shoprate-itemratinglist-name ng-binding"})
        for var in html_list:
            print(var)
        exit()
        return re_data
        
if __name__=='__main__':
    obj=GetinfoController()
    obj.index()